﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.SessionState;
using System.Web;

namespace Tools
{
    public static class SessionDictionary
    {
        public static Dictionary<string, object> GetSession(string SessionID)
        {
            return ((Dictionary<string, Dictionary<string, object>>)HttpContext.Current.Application["SessionDictionary"])[SessionID];
        }
        public static void SetSession(HttpSessionState Session, string Key, object Value)
        {
            Session[Key] = Value;
            Dictionary<string, object> S = ((Dictionary<string, Dictionary<string, object>>)HttpContext.Current.Application["SessionDictionary"])[Session.SessionID];
            HttpContext.Current.Application.Lock();
            if (S.ContainsKey(Key))
                S[Key] = Value;
            else
                S.Add(Key, Value);
            HttpContext.Current.Application.UnLock();
        }
        public static void SetSession(string SessionID, string Key, object Value)
        {
            Dictionary<string, Dictionary<string, object>> S = (Dictionary<string, Dictionary<string, object>>)HttpContext.Current.Application["SessionDictionary"];
            if (S.ContainsKey(SessionID))
            {
                HttpContext.Current.Application.Lock();
                if (S[SessionID].ContainsKey(Key))
                    S[SessionID][Key] = Value;
                HttpContext.Current.Application.UnLock();
            }
        }
        public static void RemoveSession(HttpSessionState Session, string Key)
        {
            Session.Remove(Key);
            HttpContext.Current.Application.Lock();
            ((Dictionary<string, Dictionary<string, object>>)HttpContext.Current.Application["SessionDictionary"])[Session.SessionID].Remove(Key);
            HttpContext.Current.Application.UnLock();
        }
        public static void RemoveSession(string SessionID, string Key)
        {
            HttpContext.Current.Application.Lock();
            ((Dictionary<string, Dictionary<string, object>>)HttpContext.Current.Application["SessionDictionary"])[SessionID].Remove(Key);
            HttpContext.Current.Application.UnLock();
        }
    }
}
